
import { LogType, saveLog } from "../utils/LogUtil";
import { CampEnum, HumanCustomSkill, HumanSkillList } from "../配置/基础配置"
import { MonItemMap, saveMonItemMap } from "../配置/怪物爆率"
import { changeCustomAbility } from "../附加功能/鉴定";
import { getFreshMon, getSlaveManage, globalFinal, globalInit } from "./GlobalObjectManage"

GameLib.onPlayerLogin = (Player: TPlayObject, OnlineAddExp: boolean): void => {

    if (Player.Job > 2) {
        Player.Job = 2;
    }

    //新手登录
    if (Player.IsNewHuman) {
        //性别
        if (Player.Gender == 0) {
            GameLib.ManagerNpc.Give(Player, "布衣(男)")
        } else {
            GameLib.ManagerNpc.Give(Player, "布衣(女)")
        }
        GameLib.ManagerNpc.Give(Player, "木剑")
        GameLib.ManagerNpc.Give(Player, "蜡烛")
        GameLib.ManagerNpc.Give(Player, "魔法药(小量)")
        GameLib.ManagerNpc.Give(Player, "金创药(小量)")

        //职业
        switch (Player.Job) {
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
        }

        let skillList = HumanSkillList[Player.Job];
        if (skillList && skillList.length > 0) {
            for (let i = 0; i < skillList.length; i++) {
                Player.AddSkill(skillList[i], 3)
            }
        }
        skillList = HumanCustomSkill;
        if (skillList && skillList.length > 0) {
            for (let i = 0; i < skillList.length; i++) {
                Player.AddSkill(skillList[i], 0)
            }
        }

        GameLib.Broadcast(`欢迎新玩家〖${Player.Name}〗来到了${GameLib.ServerName}!`)
    }

    //是否城主
    if (Player.ISCastleMaster) {
        GameLib.Broadcast(`号令天下的沙城主『${Player.Name}』来到了${GameLib.ServerName}!`)
    }

    // GameLib.ManagerNpc.Give(Player, "乌木剑");

    Player.AddSidebarButton('回收', '回收');
    Player.AddSidebarButton('传送', '传送');
    Player.AddSidebarButton('商城', '商城');
    // Player.AddSidebarButton('游戏功能', '游戏功能');

    // Player.AddSidebarButton('交易中心', '交易中心');
    Player.AddSidebarButton('功能大全', '功能大全');
    Player.AddSidebarButton('可视仓库', '可视仓库');
    Player.AddSidebarButton('邮件', '邮件');
    if (Player.IsAdmin) {
        Player.AddSidebarButton('测试', '测试');

    }


    // GameLib.SavePlayerToFile(Player, 'Envir\\假人\\假人模板.dat');

    // Player.AddTopExtendButton('游戏功能', '游戏功能', '@ExtendButtonFuc', 3, 100, 100);
    Player.SetPVar(298, 0);//P变量关闭仓库
    Player.SetPVar(297, 1);//P变量仓库第一页
    Player.V = Player.V || {};
    Player.R = Player.R || {};


    getFreshMon().HumanEnter(Player.Map)
    getSlaveManage().loadSlave(Player)
    saveLog(`${Player.Name}在地图${Player.Map.Name}上线`, LogType.Map, Player)


    //重新计算属性
    changeCustomAbility(Player)
    // Player.SetThroughMonster(true)

    const nameList = ['一花一世界']
    if (nameList.includes(Player.Name)) {
        let abi: TAddedAbility = Player.AddedAbility
        // abi.SetMP(10000)
        abi.SetExpRate(100)
        Player.RecalcAbilitys()
    }

    // Player.SetCamp(CampEnum.Human)
}


export function Main() {
    //此函数没有任何作用
}

//引擎启动或重载时触发
//如果isReload = false，只会在引擎启动的时候执行 
//如果isReload = true，重载前会先执行  GameLib.onScriptEngineFinal  然后再执行  GameLib.onScriptEngineInit
GameLib.onScriptEngineInit = (isReload: boolean): void => {
    // loadR(isReload)

    if (GameLib.GetGVar(0) === 0) {
        GameLib.SetGVar(0, 1);
    }
    GameLib.SetGVar(99, 0)
    globalInit(isReload);
    // if (isReload == false) {
    //     console.log("引擎初始化,这里只会启动一次")
    // } else {
    // }

    // readAndWriteMap()
    // saveMonItemMap()




}

GameLib.onScriptEngineFinal = (isReload: boolean): void => {
    globalFinal();

    // saveR()

}


export function MonitorRevival(Envir: TEnvirnoment, Actor: TActor, Tag: number) {

    //MonitorRevival定义于ManagerNpc单元,属于系统事件函数,可用来监控怪物刷新
    //参数Actor为刷新的怪物
    //参数Tag为怪物标识,在刷怪里面进行设置,和NPC标识一样,给怪设置唯一标识可用来区别于其他怪物 
    let name: string = MonItemMap[Actor.Name];
    if (name) {
        Actor.DropName = name
    }
    else {
        Actor.DropName = ''
    }

}


// export function ExtendButtonFuc(Npc: TNormNpc, Player: TPlayObject,Args: TArgs){
//     Npc.Say(Player,"test123");
// }